//
//  ReportOrCancelViewController.m
//  MyProject
//
//  Created by 石小强 on 2018/5/2.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "ReportOrCancelViewController.h"
#import "ReportOrCancelCell.h"

@interface ReportOrCancelViewController ()

@end

@implementation ReportOrCancelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupSubViews];
}

- (void)setupSubViews {
    if (self.index == 1 || self.index == 3) {
        self.navigationItem.title = @"举报";
    }
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ReportOrCancelCell class]) bundle:nil] forCellReuseIdentifier:@"ReportOrCancelCell"];
    
    self.textView.placeholder = @"请描述问题详情，以方便对方了解实际情况";
    self.bottomView.frame = CGRectMake(0, 0, kScreenWidth, 208);
    self.tableView.tableFooterView = self.bottomView;
}

- (IBAction)submitBtnClick:(id)sender {
    
    if (!self.textView.text || self.textView.text.length == 0) {
        [CommonUtils showToast:@"请描述问题详情"];
        return;
    }
    
    NSString *reportType = @"";
    for (StringAndIndexModel *model in self.dataArray) {
        if (model.index == 1) {
            reportType = model.msg;
            break;
        }
    }
    
    WS(weakSelf)
    [self showProgressWithView:self.view animated:YES];
    [SendRequest reportWithRelatedId:self.relatedId andRelatedType:self.relatedType andReportType:reportType andRemark:self.textView.text result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            // 举报成功
            [CommonUtils showToast:@"举报成功\n我们会尽快核查并及时与您反馈，感谢您对台阶的支持!"];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ReportOrCancelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReportOrCancelCell"];
    cell.model = [self.dataArray objectAtIndex:indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    StringAndIndexModel *model = [self.dataArray objectAtIndex:indexPath.row];
    model.index = model.index == 0 ? 1 : 0;
    
    if (model.index == 1) {
        for (StringAndIndexModel *other in self.dataArray) {
            if (![other.msg isEqualToString:model.msg]) {
                other.index = 0;
            }
        }
    }
    [tableView reloadData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
}

@end
